﻿using CommunityToolkit.Mvvm.Messaging;
using Microsoft.Extensions.DependencyInjection;
using System.Configuration;
using System.Data;
using System.Windows;
using VteamProjectAssistant.SettingWindow;

namespace VteamProjectAssistant
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {

        // 提供一个静态属性以便在应用程序中访问容器
        public static IServiceProvider ServiceProvider { get; private set; }

        public App()
        {
            // 配置服务
            var services = new ServiceCollection();
            ConfigureServices(services);
            ServiceProvider = services.BuildServiceProvider();
        }

        private void ConfigureServices(IServiceCollection services)
        {
            // 1. 注册服务
            //services.AddSingleton<ISettingsService, SettingsService>();
            //services.AddSingleton<IDataService, DataService>();
            // services.AddTransient<IMyTransientService, MyTransientService>();

            // 2. 注册ViewModel
            services.AddTransient<MainWindowViewModel>(); // 通常ViewModel注册为Transient
            services.AddTransient<SettingWindowViewModel>();

            // 3. 注册View (Window/Page等)
            services.AddTransient<MainWindow>(); // 通常Window注册为Transient
            services.AddTransient<SettingWindowView>();

            // 4. 注册IMessenger（CommunityToolkit.Mvvm的消息服务）
            services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);
            // 如果需要使用StrongReferenceMessenger，可以替换为：
            // services.AddSingleton<IMessenger>(StrongReferenceMessenger.Default);
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // 从容器中获取主窗口并显示
            var mainWindow = ServiceProvider.GetService<MainWindow>();
            mainWindow?.Show();
        }
    }

}
